package org.jsola.hr.web;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ArrayUtil;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.CrmMemberAddDTO;
import org.jsola.hr.dto.CrmMemberAndCustomerAddDTO;
import org.jsola.hr.dto.CrmMemberUpdateDTO;
import org.jsola.hr.query.CrmMemberQuery;
import org.jsola.hr.query.HrUserQuery;
import org.jsola.hr.service.ICrmMemberService;
import org.jsola.hr.vo.CrmMemberListVO;
import org.jsola.hr.vo.CrmMemberVO;
import org.jsola.hr.vo.HrUserVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 成员表
 *
 * @author zhr
 */
@RestController("hrCrmMemberController")
@RequestMapping(HrConstants.API_PATH + "/v1/crmMembers")
public class CrmMemberController implements ICrmMemberControllerAPI {

    @Autowired
    private ICrmMemberService crmMemberService;

    @Override
    @GetMapping("/getMyManagerList")
    public Result<List<CrmMemberVO>> getMyManagerList() {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                crmMemberService.getMyManagerList(tokenUser)
        );
    }

    @Override
    @GetMapping("/getMyManager")
    public Result<List<CrmMemberVO>> getMyManager(@PathVariable String memberId) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                crmMemberService.getMyManager(memberId,tokenUser)
        );
    }

    @Override
    @GetMapping("/getAllUser")
    public Result<List<CrmMemberVO>> getAllUser(@Valid CrmMemberQuery crmMemberQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                crmMemberService.getAllUser(crmMemberQuery,tokenUser)
        );
    }

    @Override
    @GetMapping("/getMyJurisdiction")
    public Result<Integer> getMyJurisdiction() {
        TokenUser tokenUser = UserContext.getUser();
        CrmMemberQuery crmMemberQuery = new CrmMemberQuery();
        crmMemberQuery.setUserId(tokenUser.getUserId());
        List<CrmMemberListVO> select = crmMemberService.select(crmMemberQuery, tokenUser.getSiteId());
        if(CollectionUtil.isEmpty(select)){
            return Result.success(0);
        }else {
        return Result.success(
                crmMemberService.permissionVerification(tokenUser,1)?1:2
        );
        }
    }

    @Override
    @GetMapping("/getJurisdictionRange")
    public Result<List<HrUserVO>> getJurisdictionRange(@Valid HrUserQuery hrUserQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                crmMemberService.getJurisdictionRange(hrUserQuery,tokenUser)
        );
    }

    @Override
    @PutMapping("/updateJurisdiction")
    public Result<Integer> updateJurisdiction(@Valid @RequestBody CrmMemberQuery crmMemberQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                crmMemberService.updateJurisdiction(crmMemberQuery, tokenUser)
        );
    }

    @Override
    @PostMapping("/addJurisdiction")
    public Result<CrmMemberVO> addJurisdiction(@Valid @RequestBody CrmMemberAndCustomerAddDTO crmMemberAndCustomerAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                crmMemberService.addJurisdiction(crmMemberAndCustomerAddDTO, tokenUser)
        );
    }

    @Override
    @PostMapping
    public Result<CrmMemberVO> save(@Valid @RequestBody CrmMemberAddDTO crmMemberAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增成员表权限校验

        return Result.success(
                crmMemberService.save(crmMemberAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody CrmMemberUpdateDTO crmMemberUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改成员表权限校验

        int count = crmMemberService.update(crmMemberUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{crmMemberId}")
    public Result<Integer> delete(@PathVariable String crmMemberId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除成员表权限校验

        int count = crmMemberService.deleteByIds(tokenUser, crmMemberId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] crmMemberIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除成员表权限校验

        int count = crmMemberService.deleteByIds(tokenUser, crmMemberIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{crmMemberId}")
    public Result<CrmMemberVO> findById(@PathVariable String crmMemberId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看成员表权限校验

        return Result.success(
            crmMemberService.selectById(crmMemberId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<CrmMemberListVO>> listAll(@Valid CrmMemberQuery crmMemberQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看成员表权限校验

        return Result.success(
            crmMemberService.select(crmMemberQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<CrmMemberListVO>> page(@Valid CrmMemberQuery crmMemberQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                crmMemberService.permissionVerification(tokenUser,1)?
                crmMemberService.getUserPhone(crmMemberService.selectPage(crmMemberQuery, tokenUser.getSiteId()), tokenUser):
                        null
        );
    }

}

